package per.jdz.retryer.core;

import java.util.concurrent.Executor;
import java.util.function.Predicate;

import per.jdz.retryer.listener.RetryerListener;
import per.jdz.retryer.strategy.AbstractRetryerStrategy;

public class RetryerBuilder {
    private AbstractRetryerStrategy retryerStrategy;
    private Predicate predicateResult;
    private Predicate predicateThrowsException;
    private RetryerListener retryerListener;
    private Executor executorService;

    public static RetryerBuilder newBuilder() {
        return new RetryerBuilder();
    }

    public RetryerBuilder withRetryerStrategy(AbstractRetryerStrategy retryerStrategy) {
        this.retryerStrategy = retryerStrategy;
        return this;
    }

    public RetryerBuilder retryerIfThrowsException(Predicate predicate) {
        this.predicateThrowsException = predicate;
        return this;
    }

    public RetryerBuilder retryerIfResult(Predicate predicate) {
        this.predicateResult = predicate;
        return this;
    }

    public RetryerBuilder retryerExecStrategy(Executor executorService) {
        this.executorService = executorService;
        return this;
    }

    public RetryerBuilder retryerLister(RetryerListener retryerListener) {
        this.retryerListener = retryerListener;
        return this;
    }

    public Retryer build() {
        final Retryer retryer = new Retryer(retryerStrategy, predicateResult, predicateThrowsException, retryerListener, executorService);
        return retryer;
    }
}
